package com.grantha.util;

import java.io.File;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;

import org.apache.log4j.Logger;

public class ReportGenerator {
	Logger log = Logger.getLogger(this.getClass());

	public static final int BARCODE_39 = 0;
	public static final int BOOK_COUNT_FOR_ALL_SUBJECTS = 1;
	public static final int BOOKS_BY_AUTHOR = 2;
	public static final int BOOKS_BY_STATUS = 3;
	public static final int BOOKS_BY_SUBJECT = 4;
	public static final int COMPLETE_BOOK_LIST = 5;
	public static final int COMPLETE_PATRON_LIST = 6;
	public static final int DUE_BOOKS = 7;
	public static final int INCOME_FROM_FINE = 8;
	public static final int ISSUED_BOOKS = 9;
	public static final int LIBRARY_PURCHASE_DETAILS = 10;
	public static final int OVERDUE_LETTER = 11;
	public static final int PATRON_HISTORY = 12;
	public static final int RESERVATION_LETTER = 13;
	public static final int SUBSCRIPTION_LIST = 14;

	private String reportPath;

	public ReportGenerator(String reportPath) {
		this.reportPath = reportPath;
	}

	public byte[] generate(Map<String, Object> params) {


		File reportFile = new File(reportPath);
		byte[] bytes = null;
		try {
			bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), params,
					DB.getConnection());

			return bytes;

		} catch (JRException e) {
			log.error("JasperReport Exception" + e.getMessage());
			// e.printStackTrace();

		} catch (Exception e) {
			log.error("Database Exception " + e.getMessage());
			// e.printStackTrace();
		}
		return null;
	}
}
